//从vue-router中导入这个函数
import AdminLogin from "@/views/AdminLogin.vue";
import AdminMain from "@/views/AdminMain.vue";
import MemberLogin from "@/views/MemberLogin.vue";
import SelMemberByCard from "@/views/SelMemberByCard.vue";
import AdminHome from "@/views/AdminHome.vue";
import { createRouter,createWebHistory } from "vue-router";
import MemberManager from "@/views/MemberManager.vue";
import EditMember from "@/views/EditMember.vue";
import AddMember from "@/views/AddMember.vue";
import EmployeeManager from "@/views/EmployeeManager.vue";
import EditEmployee from "@/views/EditEmployee.vue";
import AddEmployee from "@/views/AddEmployee.vue";
import EquipmentManager from "@/views/EquipmentManager.vue";
import AddEquipment from "@/views/AddEquipment.vue";
import ClassManager from "@/views/ClassManager.vue";
import ClassOrder from "@/views/ClassOrder.vue";
import AddClass from "@/views/AddClass.vue";
import GoodsManager from "@/views/GoodsManager.vue";
import AddGoods from "@/views/AddGoods.vue";
import EditGoods from "@/views/EditGoods.vue";
import MemberMain from "@/views/MemberMain.vue";
import MemberInfo from "@/views/MemberInfo.vue";
import MemberHome from "@/views/MemberHome.vue";
import EditPersonalInfo from "@/views/EditPersonalInfo.vue";
import MemberClassInfo from "@/views/MemberClassInfo.vue";
import MemberOrderClass from "@/views/MemberOrderClass.vue";
import MemberGoods from "@/views/MemberGoods.vue";
import MemberCart from "@/views/MemberCart.vue";
import EditEquipment from "@/views/EditEquipment.vue";
import GoodsOrderInfo from "@/views/GoodsOrderInfo.vue";
import EditMemberPassword from "@/views/EditMemberPassword.vue";
import GoodsOrderManager from "@/views/GoodsOrderManager.vue";

const routes = [
  {
    path: '/AdminLogin',  // 你访问的网址，比如 http://localhost:5173/adminLogin
    name: 'AdminLogin',
    component: AdminLogin
  },
  {
    path: '/MemberLogin',
    name: 'MemberLogin',
    component: MemberLogin
  },
  {
    path: '/AdminMain',
    name: 'AdminMain',
    component: AdminMain,
    // 子路由（嵌套）
    children: [
      {
        path: 'AdminHome',
        name: 'AdminHome',
        component: AdminHome
      },
      {
        path: 'SelMemberByCard',
        name: 'SelMemberByCard',
        component: SelMemberByCard
      },
      {
        path: 'EditMember',
        name: 'EditMember',
        component: EditMember
      },
      {
        path: 'MemberManager',
        name: 'MemberManager',
        component: MemberManager
      },
      {
        path: 'AddMember',
        name: 'AddMember',
        component: AddMember
      },
      {
        path: 'EmployeeManager',
        name: 'EmployeeManager',
        component: EmployeeManager
      },
      {
        path: 'EditEmployee',
        name: 'EditEmployee',
        component: EditEmployee
      },
      {
        path: 'AddEmployee',
        name: 'AddEmployee',
        component: AddEmployee
      },
      {
        path: 'EquipmentManager',
        name: 'EquipmentManager',
        component: EquipmentManager
      },
      {
        path: 'EditEquipment',
        name: 'EditEquipment',
        component: EditEquipment
      },
      {
        path: 'AddEquipment',
        name: 'AddEquipment',
        component: AddEquipment
      },
      {
        path: 'ClassManager',
        name: 'ClassManager',
        component: ClassManager
      },
      {
        path: 'ClassOrder',
        name: 'ClassOrder',
        component: ClassOrder
      },
      {
        path: 'AddClass',
        name: 'AddClass',
        component: AddClass
      },
      {
        path: 'GoodsManager',
        name: 'GoodsManager',
        component: GoodsManager
      },
      {
        path: 'AddGoods',
        name: 'AddGoods',
        component: AddGoods
      },
      {
        path: 'EditGoods',
        name: 'EditGoods',
        component: EditGoods
      },
      {
        path: 'GoodsOrderManager',
        name: 'GoodsOrderManager',
        component: GoodsOrderManager
      }
    ]
  },
  {
    path: '/MemberMain',
    name: 'MemberMain',
    component: MemberMain,
    children: [
      {
        path: 'MemberHome',
        name: 'MemberHome',
        component: MemberHome
      },
      {
        path: 'MemberInfo',
        name: 'MemberInfo',
        component: MemberInfo
      },
      {
        path: 'EditPersonalInfo',
        name: 'EditPersonalInfo',
        component: EditPersonalInfo
      },
      {
        path: 'MemberClassInfo',
        name: 'MemberClassInfo',
        component: MemberClassInfo
      },
      {
        path: 'MemberOrderClass',
        name: 'MemberOrderClass',
        component: MemberOrderClass
      },
      {
        path: 'MemberGoods',
        name: 'MemberGoods',
        component: MemberGoods
      },
      {
        path: 'MemberCart',
        name: 'MemberCart',
        component: MemberCart
      },
      {
        path: 'GoodsOrderInfo',
        name: 'GoodsOrderInfo',
        component: GoodsOrderInfo
      },
      {
        path: 'EditMemberPassword',
        name: 'EditMemberPassword',
        component: EditMemberPassword
      }
    ]
  },
  // 默认重定向
  {
    path: '/',
    redirect: '/AdminLogin'
  }

  
  // 其他页面路由，比如会员登录也可以配置在这里
]

//创建router对象
const router = createRouter({
    history:createWebHistory(),
    routes:routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  // 这些路径允许匿名访问
  const publicPages = ['/MemberLogin', '/AdminLogin']
  const isPublic = publicPages.includes(to.path)

  if (!token && !isPublic) {
    // 没有 token 且访问的不是公开页 → 跳转到登录页
    next('/MemberLogin')
  } else {
    next()
  }
})


export default router